package com.happyfamily.springboot.crowdsourcingplatform.controller.guo;

import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.*;
import com.happyfamily.springboot.crowdsourcingplatform.service.guo.FrontNeedService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/api-front/frontneed")
public class FrontNeedController
{
	@Autowired
	private FrontNeedService frontNeedService;

	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value = "雇主个人中心显示个人需求列表", notes = "雇主个人中心显示个人需求列表")
	@OperationLogDetail(detail = "雇主个人中心显示个人需求列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public TableModel getAllNeedsByPage(HttpServletRequest request, String page, String limit, String ntitle, String startDate, String endDate)
	{
		int page1 = Integer.valueOf(page);
		int limit1 = Integer.valueOf(limit);

		RowBounds rowBounds = new RowBounds((page1 - 1) * limit1, limit1);

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

		String account = securityContext.getAuthentication().getName();
		if (ntitle != null)
		{
			ntitle = "%" + ntitle + "%";
		} else
		{
			ntitle = "%%";
		}
		//		if (startDate != null)
		//		{
		//			startDate = "%" + startDate + "%";
		//		} else
		//		{
		//			startDate = "%%";
		//		}
		//		if (endDate != null)
		//		{
		//			endDate = "%" + endDate + "%";
		//		} else
		//		{
		//			endDate = "%%";
		//		}


		List<Need> list = frontNeedService.search(rowBounds, account, ntitle, startDate, endDate);

		for (int i = 0; i < list.size(); i++)
		{
			if (frontNeedService.getTenderList(list.get(i).getNid()).size() > 0)
			{
				list.get(i).setTips3("1");

			}

			//			System.out.println(list.get(i).getTips3());
		}


		TableModel tableModel = new TableModel();

		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(frontNeedService.total(account, ntitle, startDate, endDate));
		tableModel.setData(list);
		return tableModel;

	}

	@GetMapping("/workList")
	@ResponseBody
	@ApiOperation(value = "雇主个人中心显示购买的作品", notes = "雇主个人中心显示购买的作品")
	@OperationLogDetail(detail = "雇主个人中心显示购买的作品", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public TableModel getAllWorksByPage(HttpServletRequest request, String page, String limit, String wtitle, String startDate, String endDate)
	{
		int page1 = Integer.valueOf(page);
		int limit1 = Integer.valueOf(limit);

		RowBounds rowBounds = new RowBounds((page1 - 1) * limit1, limit1);

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

		String account = securityContext.getAuthentication().getName();
		if (wtitle != null)
		{
			wtitle = "%" + wtitle + "%";
		} else
		{
			wtitle = "%%";
		}
		List<Work> list = frontNeedService.searchWork(rowBounds, account, wtitle, startDate, endDate);
		TableModel tableModel = new TableModel();
		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(frontNeedService.totalWork(account, wtitle, startDate, endDate));
		tableModel.setData(list);
		return tableModel;

	}


	@GetMapping("/work")
	public String workInfo(Model model, String wid)
	{
		model.addAttribute("work", frontNeedService.getWork(wid));
		return "frontPage/work-info";
	}


	@GetMapping("/confirmServer")
	public String confirmServer(Model model, String nid)
	{
		model.addAttribute("nid", nid);
		return "frontPage/confirm-service-provider";
	}

	@GetMapping("/SpecifiedUserList")
	@ResponseBody
	@ApiOperation(value = "雇主查询获取所有的服务商和联盟", notes = "雇主查询获取所有的服务商和联盟")
	@OperationLogDetail(detail = "雇主查询获取所有的服务商和联盟", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public UserModel getSpecifiedUserList(String nid)
	{
		List<Tender> list = frontNeedService.getTenderList(Long.valueOf(nid));
		ArrayList<SysUser> users = new ArrayList<>();
		ArrayList<Alliance> alliances = new ArrayList<>();
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).getTtype().equals("个人投标"))
			{
				SysUser sysUser = frontNeedService.getUserList(Long.valueOf(list.get(i).getUserid()));
				sysUser.setTips1(String.valueOf(list.get(i).getTid()));
				users.add(sysUser);
			} else
			{

				Alliance alliance = frontNeedService.getAlliance(Long.valueOf(list.get(i).getUserid()));
				alliance.setTips1(String.valueOf(list.get(i).getTid()));
				alliances.add(alliance);
			}

		}
		UserModel userModel = new UserModel();
		userModel.setUserList(users);
		userModel.setAlliances(alliances);
		return userModel;
	}

	@GetMapping("/getServiceInfo")
	public String getServiceInfo(Model model, String uid)
	{
		model.addAttribute("user", frontNeedService.getUserList(Long.valueOf(uid)));
		return "frontPage/serviceproviders-info";
	}

	@GetMapping("/getUnionInfo")
	public String getUnionInfo(Model model, String uid)
	{

		ServerType type = frontNeedService.getType(Long.valueOf(frontNeedService.getAlliance(Long.valueOf(uid)).getAtype()));
		Alliance alliance = frontNeedService.getAlliance(Long.valueOf(uid));
		alliance.setTips1(type.getSname());
		model.addAttribute("alliance", alliance);
		return "frontPage/alliance-info";
	}

	@GetMapping("/myMoney")
	public String myMoney()
	{
		return "frontPage/mymoney-list";
	}

	@GetMapping("/moneyList")
	@ResponseBody
	@ApiOperation(value = "雇主获取交易明细", notes = "雇主获取交易明细")
	@OperationLogDetail(detail = "雇主获取交易明细", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public TableModel getMoneyList(HttpServletRequest request, String page, String limit, String startDate)
	{
		int page1 = Integer.valueOf(page);
		int limit1 = Integer.valueOf(limit);

		RowBounds rowBounds = new RowBounds((page1 - 1) * limit1, limit1);

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

		String account = securityContext.getAuthentication().getName();


		SysUser user = frontNeedService.getUser(account);

		List<StationFlow> list = frontNeedService.getMoney(rowBounds, account, startDate);

		for (int i = 0; i < list.size(); i++)
		{
			list.get(i).setTips1(user.getUname());
		}



		TableModel tableModel = new TableModel();

		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(frontNeedService.totalMoney(account, startDate));
		tableModel.setData(list);
		return tableModel;
	}

	@GetMapping("/charge")
	@ResponseBody
	@ApiOperation(value = "添加雇主充值的交易明细", notes = "添加雇主充值的交易明细")
	@OperationLogDetail(detail = "添加雇主充值的交易明细", level = 3, operationUnit = OperationUnit.USER, operationType = OperationType.INSERT)
	public String charge(HttpServletRequest request, String msg)
	{

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

		String account = securityContext.getAuthentication().getName();

		return frontNeedService.charge(account, msg);

	}

	@GetMapping("/getServerType")
	@ResponseBody
	@ApiOperation(value = "前端获取服务类型", notes = "前端获取服务类型")
	@OperationLogDetail(detail = "前端获取服务类型", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<ServerType> getServerType(String sid)
	{
		return frontNeedService.getServerType(Long.valueOf(sid));
	}

	@GetMapping("/confirmServiceProvider")
	@ResponseBody
	@ApiOperation(value = "雇主确认投标的服务商", notes = "雇主确认投标的服务商")
	@OperationLogDetail(detail = "雇主确认投标的服务商", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String confirmServiceProvider(HttpServletRequest request, String nid, String tid)
	{

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();

		return frontNeedService.confirmServiceProvider(Long.valueOf(nid), Long.valueOf(tid), account);
	}

	@GetMapping("/orderList")
	@ResponseBody
	@ApiOperation(value = "雇主获取订单列表", notes = "雇主获取订单列表")
	@OperationLogDetail(detail = "雇主获取订单列表", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public TableModel orderList(HttpServletRequest request, String page, String limit, String startDate, String endDate)
	{
		int page1 = Integer.valueOf(page);
		int limit1 = Integer.valueOf(limit);
		RowBounds rowBounds = new RowBounds((page1 - 1) * limit1, limit1);
		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		List<Orders> list = frontNeedService.searchOrders(rowBounds, account, startDate, endDate);
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String currdate = format.format(d);
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).getWorkid() == 0)
			{
				list.get(i).setTips2(frontNeedService.getNeed(list.get(i).getNeedid()).getNmoney());
				list.get(i).setTips1(frontNeedService.getParameterTypeOne(list.get(i).getPid()).getPname());
				list.get(i).setTips3(currdate);
			} else
			{

				list.get(i).setTips2(frontNeedService.getWork(String.valueOf(list.get(i).getWorkid())).getWmoney());
				list.get(i).setTips1(frontNeedService.getParameterTypeOne(list.get(i).getPid()).getPname());
				list.get(i).setTips3(currdate);
			}

		}


		TableModel tableModel = new TableModel();

		tableModel.setCode(0);
		tableModel.setMsg("");
		tableModel.setCount(frontNeedService.totalOrders(account, startDate, endDate));
		tableModel.setData(list);
		return tableModel;

	}

	@GetMapping("/payMoneyWork")
	@ResponseBody
	@ApiOperation(value = "雇主支付作品金额", notes = "雇主支付作品金额")
	@OperationLogDetail(detail = "雇主支付作品金额", level = 5, operationUnit = OperationUnit.USER, operationType = OperationType.ALL)
	public String payMoney(HttpServletRequest request, String wid)
	{

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		return frontNeedService.payMoneyWork(account, wid);
	}

	@GetMapping("/payMoneyNeed")
	@ResponseBody
	@ApiOperation(value = "雇主支付需求金额", notes = "雇主支付需求金额")
	@OperationLogDetail(detail = "雇主支付需求金额", level = 5, operationUnit = OperationUnit.USER, operationType = OperationType.ALL)
	public String payMoneyNeed(HttpServletRequest request, String nid, String oid)
	{

		SecurityContextImpl securityContext = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContext.getAuthentication().getName();
		return frontNeedService.payMoneyNeed(Long.valueOf(nid), account, Long.valueOf(oid));
	}


	@GetMapping(value = "/guoneedverify")
	public String verifyNeed(Model model, String nid)
	{
		model.addAttribute("need", frontNeedService.getNeed(Long.valueOf(nid)));
		return "frontPage/guoneed-verify";
	}


	@GetMapping("/getCounltantUsder")
	@ResponseBody
	@ApiOperation(value = "前端获取顾问展示", notes = "前端获取顾问展示")
	@OperationLogDetail(detail = "前端获取顾问展示", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public List<SysUser> getCounltantUsder()
	{
		return frontNeedService.getCounltantUsder(Long.valueOf(7));
	}
}
